// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RoutedCommand.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Windows.Input.Commands
{
    using System;
    using System.Windows.Input;

    public class RoutedCommand : ICommand
    {
        #region Fields

        private readonly string _name;
        private readonly Type _ownerType;

        #endregion

        public RoutedCommand(string name, Type ownerType)
        {
            _name = name;
            _ownerType = ownerType;
        }

        public string Name
        {
            get { return _name; }
        }

        public Type OwnerType
        {
            get { return _ownerType; }
        }

        public bool CanExecute(object context, object parameter)
        {
            var router = CommandManager.GetCommandRouter(this);
            return router != null && router.CanExecute(this, context, parameter);
        }

        public void Execute(object context, object parameter)
        {
            var router = CommandManager.GetCommandRouter(this);
            if (router != null)
            {
                router.Execute(this, context, parameter);
            }
        }

        bool ICommand.CanExecute(object parameter)
        {
            return CanExecute(null, parameter);
        }

        void ICommand.Execute(object parameter)
        {
            Execute(null, parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}